package com.fenxiangle.yueding.feature.focus.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.WxUserBo;
import com.fenxiangle.yueding.feature.login.view.LoginActivity;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Home on 2018/9/11.
 */

public class BindWeChatActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.btn_weichat)
    TextView btnWeichat;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_binding_weichat;
    }

    @Override
    protected void initInjector() {

    }
    private UMShareAPI mShareAPI;
    @Override
    protected void initData() {
        initToolBar(toolbar, "绑定微信", true, true);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        mShareAPI = UMShareAPI.get(this);
        mShareAPI.setShareConfig(config);
        btnWeichat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initUmLoginWx(SHARE_MEDIA.WEIXIN);
            }
        });
    }

    private void initUmLoginWx(final SHARE_MEDIA type) {
        try {
            if (!mShareAPI.isInstall(this, SHARE_MEDIA.WEIXIN)) {
                T.showShort("您未安装微信！");
                return;
            }
        } catch (Exception e) {
            showMsg(e.getMessage());
        }


        mShareAPI.doOauthVerify(this, type, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {


                mShareAPI.getPlatformInfo(BindWeChatActivity.this, type, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        showLoading();
                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        if (map == null) return;
                        WxUserBo mWxUserInfoBo = JSONObject.parseObject(JSON.toJSONString(map), WxUserBo.class);
                        Intent intent=new Intent();
                        intent.putExtra("openid",mWxUserInfoBo.getOpenid());
                        setResult(30005,intent);
                        finish();
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        dismissLoading();
                        T.showShort(throwable.getMessage());
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {
                        dismissLoading();
                    }
                });

            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
